<?php

/**************************************
INDEX

PHP INCLUDES
JS & CSS INCLUDES
ADD_THEME_SUPPORT CALLS
IMAGE SIZES
REGISTER MAIN MENU
LOCALIZATION INIT
REGISTER WIDGET AREAS
MEDIA UPLOAD CUSTOMIZE

***************************************/

/**************************************
PHP INCLUDES
***************************************/

	include 'inc/functions/functions_custom.php';
	include 'inc/functions/functions_tgm.php';
	include 'inc/functions/functions_font_awesome.php';
	include 'inc/functions/functions_google_webfonts.php';

	//options
	include 'inc/options/options_general_control.php';
	include 'inc/options/options_hp_control.php';
	include 'inc/options/options_slider_control.php';
	include 'inc/options/options_post_control.php';
	include 'inc/options/options_appearance_control.php';
	include 'inc/options/options_hooks_control.php';
	include 'inc/options/options_help_control.php';

	//framework
	include 'inc/framework/fw_index.php';


/**************************************
JS & CSS INCLUDES
***************************************/

	//front end includes
	add_action('wp_enqueue_scripts','canon_load_to_front');
	function canon_load_to_front() {
		//get options
		$canon_options = get_option('canon_options');
		$canon_options_hp = get_option('canon_options_hp');
		$canon_options_slider = get_option('canon_options_slider');
		$canon_options_post = get_option('canon_options_post');
		$canon_options_appearance = get_option('canon_options_appearance');

		//wp scripts
		wp_enqueue_script('jquery', false, array(), false, true);

		//external scripts
		wp_enqueue_script('modernizr', get_template_directory_uri(). '/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js', array(), false, false);
		if (is_page_template('page-gallery.php')) { wp_enqueue_script('isotope', get_template_directory_uri(). '/js/jquery.isotope.min.js', array(), false, true); }
		wp_enqueue_script('flexslider', get_template_directory_uri(). '/js/jquery.flexslider-min.js', array(), false, true);
		wp_enqueue_script('fitvids', get_template_directory_uri(). '/js/fitvids.min.js', array(), false, true);
		if (is_page_template('page-homepage.php')) { wp_enqueue_script('backstretch', get_template_directory_uri(). '/js/backstretch.js', array(), false, true); }
		wp_enqueue_script('placeholder', get_template_directory_uri(). '/js/placeholder.js', array(), false, true);
		wp_enqueue_script('mosaic', get_template_directory_uri(). '/js/mosaic.1.0.1.min.js', array(), false, true);

		wp_enqueue_script('fancybox_mousewheel', get_template_directory_uri() . '/js/fancybox/lib/jquery.mousewheel-3.0.6.pack.js', array('jquery'), false, true);
		wp_enqueue_script('fancybox_core', get_template_directory_uri() . '/js/fancybox/source/jquery.fancybox.pack.js', array('jquery'), false, true);
		wp_enqueue_script('fancybox_buttons', get_template_directory_uri() . '/js/fancybox/source/helpers/jquery.fancybox-buttons.js', array('fancybox_core'), false, true);
		wp_enqueue_script('fancybox_media', get_template_directory_uri() . '/js/fancybox/source/helpers/jquery.fancybox-media.js', array('fancybox_core'), false, true);
		wp_enqueue_script('fancybox_thumbs', get_template_directory_uri() . '/js/fancybox/source/helpers/jquery.fancybox-thumbs.js', array('fancybox_core'), false, true);

		wp_enqueue_script('canon_cleantabs', get_template_directory_uri(). '/js/cleantabs.jquery.js', array(), false, true);
		wp_enqueue_script('canon_easing', get_template_directory_uri(). '/js/jquery.easing.1.3.js', array(), false, true);
		wp_enqueue_script('canon_superslides', get_template_directory_uri(). '/js/jquery.superslides.js', array(), false, true);
		wp_enqueue_script('canon_hammer', get_template_directory_uri(). '/js/hammer.js', array(), false, true);
		wp_enqueue_script('canon_hammer_specialevent', get_template_directory_uri(). '/js/jquery.specialevent.hammer.js', array(), false, true);

		//canon scripts
		wp_enqueue_script('canon_global_functions', get_template_directory_uri(). '/js/global_functions.js', array(), false, true);
		wp_enqueue_script('canon_scripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), false, true);


		//support for threaded comments
		if (is_singular() && get_option('thread_comments'))	wp_enqueue_script('comment-reply');
		
		//styles (css)
		wp_enqueue_style('woo_commerce_style', get_template_directory_uri(). '/css/woo-shop.css');
		wp_enqueue_style('normalize', get_template_directory_uri(). '/css/normalize.min.css');
		wp_enqueue_style('style', get_stylesheet_uri());
		if (isset($canon_options['use_responsive_design'])) { if ($canon_options['use_responsive_design'] == "checked") { wp_enqueue_style('responsive_style', get_template_directory_uri(). '/css/responsive.css'); } }
		
		wp_enqueue_style('isotope_style', get_template_directory_uri(). '/css/isotope.css');
		wp_enqueue_style('flexslider_style', get_template_directory_uri(). '/css/flexslider.css');
		wp_enqueue_style('font_awesome_style', get_template_directory_uri(). '/css/font-awesome.css');
		
		wp_enqueue_style('fancybox_style', get_template_directory_uri(). '/js/fancybox/source/jquery.fancybox.css');
		wp_enqueue_style('fancybox_buttons_style', get_template_directory_uri(). '/js/fancybox/source/helpers/jquery.fancybox-buttons.css');
		wp_enqueue_style('fancybox_thumbs_style', get_template_directory_uri(). '/js/fancybox/source/helpers/jquery.fancybox-thumbs.css');

		wp_enqueue_style('canon_superslides', get_template_directory_uri(). '/css/superslides.css');
		
		//localize sripts
		wp_localize_script('canon_scripts','extData', array(
		 	'ajaxUrl' 					=> admin_url('admin-ajax.php'), 
			'pageType'					=> mb_get_page_type(), 
			'templateURI' 				=>  get_template_directory_uri(), 
			'canonOptions' 				=> $canon_options,
			'canonOptionsHp' 			=> $canon_options_hp,
			'canonOptionsSlider' 		=> $canon_options_slider,
			'canonOptionsPost' 			=> $canon_options_post,
			'canonOptionsAppearance' 	=> $canon_options_appearance,
		)); 
	}

	//back end includes
	add_action('admin_enqueue_scripts', 'canon_load_to_back');  //this was changed to admin_enqueue_scripts from action hook admin_footer. Let's see if it makes a difference
	function canon_load_to_back() {
		//scripts (js)
		wp_enqueue_script('jquery');
		wp_enqueue_script('jquery-ui', false, array(), false, true);
		wp_enqueue_script('jquery-ui-sortable', false, array(), false, true);
		wp_enqueue_script('thickbox', false, array(), false, true);					
		wp_enqueue_script('media-upload', false, array(), false, true);
		wp_enqueue_script('canon_colorpicker', get_template_directory_uri() . '/js/colorpicker.js', array(), false, true);
		wp_enqueue_script('canon_backend_scripts', get_template_directory_uri() . '/js/backend_scripts.js', array(), false, true);
		wp_enqueue_script('canon_options_slider_scripts', get_template_directory_uri() . '/js/options_slider.js', array(), false, true);

		//style (css)	
		wp_enqueue_style('thickbox');
		wp_enqueue_style('font_awesome_backend_style', get_template_directory_uri(). '/css/font-awesome.css');
		wp_enqueue_style('canon_backend_style', get_template_directory_uri(). '/css/backend.css');
		wp_enqueue_style('canon_options_slider_style', get_template_directory_uri(). '/css/options_slider.css');
		wp_enqueue_style('canon_colorpicker_style', get_template_directory_uri(). '/css/colorpicker.css');

		//localize sripts
		$canon_options = get_option('canon_options');
    	if (get_current_screen()->id == 'kodax-settings_page_handle_canon_options_appearance') wp_localize_script('canon_backend_scripts','extDataFonts', array('fonts' => mb_get_google_webfonts()));        
		wp_localize_script('canon_backend_scripts','extData', array(
			'templateURI'		=> get_template_directory_uri(), 
			'ajaxURL'			=> admin_url('admin-ajax.php'),
			'canonOptions'		=> $canon_options
		));        

	}


/**************************************
ADD_THEME_SUPPORT CALLS
***************************************/

	// Add default posts and comments RSS feed links to <head>.
	add_theme_support( 'automatic-feed-links' );

	// This theme uses Featured Images
	add_theme_support( 'post-thumbnails' );

	//post formats
	add_theme_support('post-formats', array('quote','gallery','video','audio'));

	// woocommerce
	add_theme_support( 'woocommerce' );

/**************************************
IMAGE SIZES
***************************************/

	// add_image_size( 'gallery_small', 338, 197, true);
	// add_image_size( 'gallery_small_2x', 676, 394, true);
	// add_image_size( 'gallery_full_small_2x', 664, 388, true);
	add_image_size( 'gallery_isotope_x2', 466, 304, true);

	//set general content width
	if (!isset($content_width)) $content_width = 741;


/**************************************
REGISTER MAIN MENU
***************************************/

	//register main menu
	register_nav_menus(array(
			'main_navigation_menu' => 'Main Navigation Menu'
	)); 

/**************************************
LOCALIZATION INIT
***************************************/

	add_action('after_setup_theme', 'canon_localization_setup');
	function canon_localization_setup() {
		$lang_dir = get_template_directory() . '/lang';    
		load_theme_textdomain('loc_canon', $lang_dir);
	} 

/**************************************
REGISTER WIDGET AREAS
***************************************/

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_blog_sidebar_widget_area",
		    'name' => 'Blog/Category Sidebar Widget Area',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div><hr class="dots"/>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_page_sidebar_widget_area",
		    'name' => 'Page Sidebar Widget Area',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div><hr class="dots"/>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_404_sidebar_widget_area",
		    'name' => '404 Sidebar Widget Area',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div><hr class="dots"/>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_contact_sidebar_widget_area",
		    'name' => 'Contact Sidebar Widget Area',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div><hr class="dots"/>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_footer_widget_area_1",
		    'name' => 'Footer: Widget Area 1',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_footer_widget_area_2",
		    'name' => 'Footer: Widget Area 2',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_footer_widget_area_3",
		    'name' => 'Footer: Widget Area 3',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_footer_widget_area_4",
		    'name' => 'Footer: Widget Area 4',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar')) {
	    register_sidebar(array(  
	    	'id' => "canon_footer_widget_area_5",
		    'name' => 'Footer: Widget Area 5',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }

	if (function_exists('register_sidebar') && class_exists('Woocommerce')) {
	    register_sidebar(array(  
	    	'id' => "canon_woocommerce_widget_area",
		    'name' => 'WooCommerce Widget Area',  
		    'before_widget' => '<div id="%1$s" class="widget %2$s">',  
		    'after_widget' => '</div><hr class="dots"/>',  
		    'before_title' => '<h2 class="widget-title">',  
		    'after_title' => '</h2>'
		)); 
	 }


/**************************************
MEDIA UPLOAD CUSTOMIZE
***************************************/

	add_action( 'admin_init', 'check_upload_page' );
	function check_upload_page() {
		global $pagenow;
		if ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
			// Now we'll replace the 'Insert into Post Button' inside Thickbox
			add_filter( 'gettext', 'replace_thickbox_text', 1, 3 );
		}
	}

	function replace_thickbox_text($translated_text, $text, $domain) {
		if ('Insert into Post' == $text) {

			return __("Use this image", "loc_canon");
			
		}
		return $translated_text;
	}


